package com.ymatou.iapi.optrade.testcase;

import java.util.Arrays;

import org.joda.time.DateTime;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.CustomsClearanceUnfreezeOrderReq;
import com.ymatou.iapi.optrade.parameter.utill.Utils;
import com.ymatou.iapi.optrade.resp.Order;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.CustomsClearanceCall;
import com.ymatou.iapi.optrade.service.RefundService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.database.sharding.db.sharding.model.AutoReceiveOptPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.TestDataManager;

/**
 * 贝海清关解冻接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_CustomsClearance {

	private static CustomsClearanceUnfreezeOrderReq customsClearanceUnfreezeOrderReq;
	private static CustomsClearanceCall customsClearanceCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("贝海清关解冻接口");
	}

	@Before
	public void caseUp() {
		customsClearanceUnfreezeOrderReq = new CustomsClearanceUnfreezeOrderReq();
		customsClearanceCall = new CustomsClearanceCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(customsClearanceCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 校验自动确认收货时间操作表（auto_receive_opt） 
	 */
	private void verifyAutoReceive(long orderId, boolean isFreeze) {

		//  自动确认收货时间操作表（auto_receive_opt） 
		AutoReceiveOptPo autoReceiveOptPo = new OrderShardingWapper()
				.selectAutoReceiveOptByOrderId(orderId);

		Logger.comment(String.format(
				"验证自动确认收货时间操作表（auto_receive_opt）  orderId: %s", orderId));

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getAddTime()) < 2,
				"验证数据库AddTime跟系统当前时间差为2分钟内");
		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getUpdateTime()) < 2,
				"验证数据库UpdateTime跟系统当前时间差为2分钟内");

		Logger.verifyEquals(orderId, autoReceiveOptPo.getOrderId(),
				"验证数据库OrderId");
		Logger.verifyEquals(isFreeze, autoReceiveOptPo.getFreeze(),
				"验证数据库Freeze");

		Logger.verifyEquals(true,
				Utils.calcTimeSpanMin(autoReceiveOptPo.getFreezeTime()) < 2,
				"验证数据库FreezeTime跟系统当前时间差为2分钟内");

		//贝海直邮订单清关后，自动确认收货为7天后
		Logger.verifyEquals(true, Utils.daysBetween(DateTime.now().plusDays(7)
				.toDate(), autoReceiveOptPo.getExpectReceiveTime()) < 1,
				"验证数据库ExpectReceiveTime");

	}
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_CustomsClearance_001() {
		Logger.start(true, "贝海直邮订单的清关成功,清关后解冻自动确认收货");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderDelivery();

			Long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			String billNo = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			// 码头保存物流单号
			TradingCallService.saveDeliverSummary(orderId, sellerId, billNo);

			customsClearanceUnfreezeOrderReq.setLogisticsNos(Arrays
					.asList(billNo));

			customsClearanceCall.setData(customsClearanceUnfreezeOrderReq);

			customsClearanceCall.callService();

			Logger.verifyEquals("ok", customsClearanceCall.getReturnData(),
					"验证Code");

			// 订单主表
			verifyAutoReceive(orderId, false);

		} catch (Exception e) {

		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	@Ignore
	public void Tc_CustomsClearance_002() {
		Logger.start(true, "贝海直邮订单的订单,有退款冻结,清关后 自动收货不解冻");
		try {

			TradingServiceResp placeOrderResp = TradingCallService
					.orderDelivery();

			Order order = placeOrderResp.getOrderList().get(0);

			long orderId = order.getOrderId();
			long sellerId = order.getSellerId();

			//申请退款
			RefundService.applyRefund(order);

			String billNo1 = String.format("YD%s",
					TestDataManager.getRandomNum(10));

			String billNo2 = String.format("YD%s",
					TestDataManager.getRandomNum(10));
			// 码头保存物流单号
			TradingCallService.saveDeliverSummary(orderId, sellerId, billNo1);
			TradingCallService.saveDeliverSummary(orderId, sellerId, billNo2);

			customsClearanceUnfreezeOrderReq.setLogisticsNos(Arrays.asList(
					billNo1, billNo2));

			customsClearanceCall.setData(customsClearanceUnfreezeOrderReq);

			customsClearanceCall.callService();

			Logger.verifyEquals("ok", customsClearanceCall.getReturnData(),
					"验证Code");

			// 订单主表
			verifyAutoReceive(orderId, true);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}
